const Controller = require('egg').Controller

class SettingController extends Controller {
      // 获取启用
      async getActiveSetting() {
        try {
            const { ctx } = this
            // const setting = await ctx.model.Setting.findOne({
            //   where: {
            //     active: true
            //   }
            // })
            // 使用raw sql
            const setting = await ctx.model.query('select * from settings where active = true', {type:'SELECT'})
            ctx.body = {
                code: '0',
                desc: '主页获取成功',
                setting: setting[0]
            }
        } catch(error) {
            const { ctx } = this
            ctx.body = {
                code: '-1',
                desc: '主页获取失败',
                err: error
            }
        }
    }

    async getSettings() {
        try {
            const { ctx } = this;
            const { page, limit } = ctx.request.body;
            const pageOffset = ctx.helper.toInt((page - 1) * limit);
            const pageLimit = ctx.helper.toInt(limit);
            const settings = await ctx.model.Setting.findAndCountAll({
                limit: pageLimit,
                offset: pageOffset
            });
            ctx.body = {
                code: '0',
                desc: '设置获取成功',
                settings: settings,
                count: settings.count
            };
        } catch(error) {
            const { ctx } = this;
            ctx.body = {
                code: '-1',
                desc: '设置获取失败',
                err: error
            };
        }
    }

    // 获取指定
    async getSetting() {
        try {
            const { ctx } = this
            const { id } = ctx.request.body
            const setting = await ctx.model.Setting.findByPk(id)
            ctx.body = {
                code: '0',
                desc: '设置获取成功',
                setting: setting
            }
        } catch(error) {
            const { ctx } = this
            ctx.body = {
                code: '-1',
                desc: '设置获取失败',
                err: error
            }
        }
    }

    async createSetting() {
        try {
            const { ctx } = this
            const { title, content } = ctx.request.body
            await ctx.model.Setting.create({
              title,
              content
            })
            ctx.body = {
              code: '0',
              desc: '设置创建成功'
            }
        } catch(error) {
            const { ctx } = this
            ctx.body = {
              code: '-1',
              desc: '设置创建失败',
              err: error
            }
        }
    }

    async editSetting() {
        try {
          const { ctx } = this
          const { id, title, content } = ctx.request.body
          // 待做：需更新updatedAt
          await ctx.model.Setting.update({
              title,
              content
          }, {
            where: {
              id
            }
          })
          ctx.body = {
            code: '0',
            desc: '设置修改成功'
          }
        } catch(error) {
          const { ctx } = this
          return ctx.body = {
            code: '-1',
            desc: '设置修改失败',
            err: error
          }
        }
    }

    async deleteSetting() {
        try {
          const { ctx } = this
          const { id } = ctx.request.body
          await ctx.model.Setting.destroy({
            where: {
              id: id
            }
          })
          ctx.body = {
            code: '0',
            desc: '设置删除成功'
          }
        } catch(error) {
          const { ctx } = this
          ctx.body = {
            code: '-1',
            desc: '设置删除失败',
            err: error
          }
        }
    }

    async batchDeleteSettings() {
        try {
            const { app, ctx } = this
            const { Op } = app.Sequelize
            const { ids } = ctx.request.body

            await ctx.model.Setting.destroy({
                where: {
                    id: {
                        [Op.in]: ids
                    }
                }
            })

            ctx.body = {
                code: '0',
                desc: '设置批量删除成功'
            }
        } catch(error) {
            const { ctx } = this
            ctx.body = {
                code: '-1',
                desc: '设置批量删除失败'
            }
        }
    }

    async changeSettingStatus() {
      let t
      try {
          const { app, ctx } = this
          const { id, status } = ctx.request.body
          const { Op } = app.Sequelize
          t = await ctx.model.transaction()
          // 设置
          let i = Number(id)
          await ctx.model.Setting.update({
            active: status
          }, {
            where: {
              id: i
            }
          }, { transaction: t })
          if(status) {
            await ctx.model.Setting.update({
              active: false
            }, {
              where: {
                id: {
                  [Op.ne]: i
                }
              }
            }, { transaction: t })
          }
          await t.commit()
          ctx.body = {
            code: '0',
            desc: '设置状态修改成功'
          }
      } catch(error) {
          await t.rollback()
          const { ctx } = this
          ctx.body = {
              error: error,
              code: '-1',
              desc: '设置状态修改失败'
          }
      }
  }
}

module.exports = SettingController